package org.gdstash.db;

import org.gdstash.file.ARZRecord;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class DBEngineSkillMaster {
    private static DBEngineSkillMaster singleton;
    private List<String> classTableIDs;

    public DBEngineSkillMaster() {
        this.classTableIDs = new LinkedList<String>();
    }

    public DBEngineSkillMaster(final ARZRecord record) {
        this.classTableIDs = record.getSkillMasterList();
    }

    public List<String> getClassTableList() {
        return this.classTableIDs;
    }

    public static void createTables() throws SQLException {
        try (final Connection conn = GDDBData.getConnection()) {
            final boolean auto = conn.getAutoCommit();
            conn.setAutoCommit(false);
            try {
                DBEngineSkillMasterAlloc.createTable(conn);
            } catch (SQLException ex) {
                conn.rollback();
                final Object[] args = {"GDC_SKILL_MASTER"};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
                GDMsgLogger.addError(msg);
                throw ex;
            } finally {
                conn.setAutoCommit(auto);
            }
        }
    }

    public static void insert(final ARZRecord record) throws SQLException {
        DBEngineSkillMaster.singleton = null;
        final DBEngineSkillMaster entry = get();
        if (entry != null && entry.classTableIDs != null && !entry.classTableIDs.isEmpty()) {
            return;
        }
        final DBEngineSkillMaster skillMaster = new DBEngineSkillMaster(record);
        if (skillMaster.classTableIDs == null) {
            return;
        }
        if (skillMaster.classTableIDs.isEmpty()) {
            return;
        }
        try (final Connection conn = GDDBData.getConnection()) {
            final boolean auto = conn.getAutoCommit();
            conn.setAutoCommit(false);
            try {
                DBEngineSkillMasterAlloc.insert(conn, skillMaster);
            } catch (SQLException ex) {
                conn.rollback();
                final Object[] args = {record.getFileName(), "GDC_SKILL_MASTER"};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_INS_TABLE_BY_ID", args);
                GDMsgLogger.addLowError(msg);
                GDMsgLogger.addLowError(ex);
            } finally {
                conn.setAutoCommit(auto);
            }
        }
    }

    public static DBEngineSkillMaster get() {
        if (DBEngineSkillMaster.singleton != null) {
            return DBEngineSkillMaster.singleton;
        }
        DBEngineSkillMaster.singleton = new DBEngineSkillMaster();
        DBEngineSkillMaster.singleton.classTableIDs = DBEngineSkillMasterAlloc.get();
        return DBEngineSkillMaster.singleton;
    }

    public static void reset() {
        DBEngineSkillMaster.singleton = null;
    }
}
